#!/usr/bin/env perl
use strict;

use File::Basename;
use File::Copy;
my @files=glob (dirname($0)."/*.jpg");
my $delay=$ARGV[0];
$delay=1 unless defined $delay;

my %sorted;
foreach (@files){
	next unless /_(\d{6})_(\d{3})/;
	next if /thumbnail/;
	my $time=$1+$2/1000;
	rename $_, "$_.save";
	push @{$sorted{$time}}, $_;
}
foreach (sort {$a <=> $b} keys %sorted){
	print "--------- $_ ---------\n";
	foreach (@{$sorted{$_}}){
		copy "$_.save", $_;
		unlink "$_.save";
		print "$_\n";
	}
	sleep $delay;
}